package com.srwl.redpacketui.ui.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.srwl.mytx.domain.User;
import com.srwl.mytx.R;
import com.srwl.mytx.fragment.BaseFragment;


public class SendPacketBaseFragment extends BaseFragment implements View.OnClickListener {


    public final static String ARGS_TARGET_USER_ID = "targetId";
    public final static String ARGS_TARGET_USER = "targetUser";

    public String[] mGreetingArray;

    public int mArrayIndex;

    private PopupWindow mPopupWindow;

    public View mPopupParent;

    public TextView mTvPopupMsg;

    public double mSingleLimit = 1000;

    public double mMinLimit = 0.01;

    private View mPopupView;

    private Button mButton;
    protected String targetId;
    protected User targetUser;
    private ProgressBar progressBar;


    protected View getLoadingTargetView(View view) {
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            targetId = getArguments().getString(ARGS_TARGET_USER_ID);
            targetUser = getArguments().getParcelable(ARGS_TARGET_USER);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getContentViewLayoutID() != 0) {
            return inflater.inflate(getContentViewLayoutID(), null);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewsAndEvents(view, savedInstanceState);

    }

    protected void initViewsAndEvents(View view, Bundle savedInstanceState) {
        //初始化设置项
        mGreetingArray = getResources().getStringArray(R.array.greetings);
        progressBar = view.findViewById(R.id.progress_bar);
        mArrayIndex++;

    }

    protected int getContentViewLayoutID() {
        return 0;
    }


    public void initPopupWindow() {
        if (mPopupView == null) {
            mPopupView = getActivity().getLayoutInflater().inflate(R.layout.rp_popup_layout, null, false);
        }
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(mPopupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mTvPopupMsg = (TextView) mPopupView.findViewById(R.id.tv_popup_msg);
            mPopupWindow.setOutsideTouchable(false);
        }
    }


    public void showPopupWindow(View parentView, TextView tvPopupMsg, String message) {
        tvPopupMsg.setText(message);
        int[] location = new int[2];
        parentView.getLocationOnScreen(location);
        int offsetY = location[1] + parentView.getHeight();
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            mPopupWindow.showAtLocation(parentView, Gravity.START | Gravity.TOP, 0, offsetY);
        }
    }

    public void hidePopupWindow() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

//    public void updateLimit() {
//        mSingleLimit = Double.valueOf(RPPreferenceManager.getInstance().getLimit());
//        mMinLimit = Double.valueOf(RPPreferenceManager.getInstance().getMinLimit());
//    }

    /**
     * 把200.00变成200
     */
    public String getNumberLimit(double number) {
        String numberLimit = String.valueOf(number);
        if (number >= 1) {
            int singleLimitPosDot = numberLimit.indexOf(".");
            if (singleLimitPosDot > 0) {
                String[] st = numberLimit.split("\\.");
                numberLimit = st[0];
            }
        }
        return numberLimit;
    }

    /**
     * 判断字符串前面有几个0开头
     */
    public int getStartZeroNumber(String num) {
        double money = Double.valueOf(num);
        if (money == 0) {
            return num.length();
        } else {
            return num.length() - String.valueOf(Integer.parseInt(num)).length();
        }
    }


    public void enableButton(Button button) {
        button.setEnabled(true);
    }

    public void disableButton(Button button) {
        mButton = button;
        button.setEnabled(false);
    }


    @Override
    public void onClick(View v) {

    }

//    private void showTipDialog(String code, String msg, PayTipsDialogFragment.OnDialogConfirmClickCallback callback) {
//        PayTipsDialogFragment dialog = PayTipsDialogFragment.newInstance(code, msg);
//        dialog.setCallback(callback);
//        if (getActivity() != null) {
//            FragmentTransaction ft = getMyFragmentManager(getActivity()).beginTransaction();
//            ft.add(dialog, RPConstant.RP_TIP_DIALOG_TAG);
//            ft.commitAllowingStateLoss();
//        }
//    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePopupWindow();
        hideSoftKeyboard();
    }

//    @Override
//    public void onDetach() {
//        super.onDetach();
//
//
//    }


    protected void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }


    protected void hideLoading() {
        progressBar.setVisibility(View.GONE);

    }
}
